<!DOCTYPE html>
<html xmlns:th="www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('登录')"/>
    <link rel="stylesheet" th:href="@{/layuimini/css/login.css}" />

</head>
<body>
<div class="main-body">
    <div class="login-main">
        <div class="login-top">
            <span>登录</span>
            <span class="bg1"></span>
            <span class="bg2"></span>
        </div>
        <form class="layui-form login-bottom">
            <div class="center">
                <div class="item">
                    <span class="icon icon-2"></span>
                    <input type="text" name="username" lay-verify="required"  placeholder="账号" maxlength="24"/>
                </div>

                <div class="item">
                    <span class="icon icon-3"></span>
                    <input type="password" name="password" lay-verify="required"  placeholder="密码" maxlength="20">
                    <span class="bind-password icon icon-4"></span>
                </div>

                <div id="validatePanel" class="item" style="width: 137px;">
                    <input type="text" name="captcha" placeholder="请输入验证码" maxlength="4" hover  lay-verify="required" />
                    <img id="captchaImage" th:src="@{/generate}" class="validateImg" >
                </div>

            </div>
            <div class="tip">
                <input type="hidden" name="remember-me" id="remember" value="0"/>
                <span class="icon-nocheck"></span>
                <span class="login-tip">保持登录</span>
                <a href="javascript:" class="forget-password">忘记密码？</a>
            </div>
            <div class="layui-form-item" style="text-align:center; width:100%;height:100%;margin:0px;">
                <button class="login-btn" lay-submit="" lay-filter="login">立即登录</button>
            </div>
        </form>
    </div>
</div>
<div class="footer">
   <!-- ©版权所有 2014-2018 叁贰柒工作室<span class="padding-5">|</span><a target="_blank" href="http://www.miitbeian.gov.cn">粤ICP备16006642号-2</a>
--></div>

<th:block th:include="include :: footer"/>
<script>
    layui.use(function () {
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer;
        let captchaPath = pName + "/generate";

        // 登录过期的时候，跳出ifram框架
        if (top.location != self.location) top.location = self.location;

        $('.bind-password').on('click', function () {
            if ($(this).hasClass('icon-5')) {
                $(this).removeClass('icon-5');
                $("input[name='password']").attr('type', 'password');
            } else {
                $(this).addClass('icon-5');
                $("input[name='password']").attr('type', 'text');
            }
        });

        $('.icon-nocheck').on('click', function () {
            if ($(this).hasClass('icon-check')) {
                $("#remember").val(0);
                $(this).removeClass('icon-check');
            } else {
                $(this).addClass('icon-check');
                $("#remember").val(1);
            }
        });

        // 进行登录操作
        form.on('submit(login)', function (data) {

            if (data.username == '') {
                layer.msg('用户名不能为空');
                return false;
            }
            if (data.password == '') {
                layer.msg('密码不能为空');
                return false;
            }
            if (data.captcha == '') {
                layer.msg('验证码不能为空');
                return false;
            }
            let loader = layer.load();
            $.ajax({
                url: 'login',
                data: data.field,
                type: "post",
                dataType: 'json',
                success: function (result) {
                    localStorage.setItem("currentUser", JSON.stringify(result.data ));
                    layer.close(loader);
                    if (result.code == 1) {
                        layer.msg(result.msg, {icon: 1, time: 500}, function () {
                            location.href = "index";
                        })
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1500}, function (index) {
                            document.getElementById("captchaImage").src = captchaPath + "?" + Math.random();
                            layer.close(index);
                        });
                    }

                }
            });
            return false;
        });

        $("#captchaImage").click(function () {
            document.getElementById("captchaImage").src = captchaPath + "?" + Math.random();
        })

    });
</script>
</body>
</html>